package com.alex.service;

import com.alex.dao.AccountMapper;
import com.alex.pojo.Account;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("accountService")
@Transactional
public class AccountServiceImpl implements AccountService {

    private final AccountMapper accountMapper;

    public AccountServiceImpl(AccountMapper accountMapper) {
        this.accountMapper = accountMapper;
    }

    @Override
    public boolean moveAccount(int fromId, int toId, int m) {
        Account fromAccount = accountMapper.getAccount(fromId);
        Account toAccount = accountMapper.getAccount(toId);
        accountMapper.updateAccount(fromId, fromAccount.getAccount()-m);
        if (fromAccount.getAccount()-m < 0) {
            throw  new RuntimeException("转账失败!余额不足!");
        }
        accountMapper.updateAccount(toId, toAccount.getAccount()+m);
        return true;
    }
}
